//内部评审后面的都可以修改
var defaultParameter = {
  pageNumber: 1,
  pageSize: 20
};

var internalParty="";
var internalPartyName="";
var expertParty="";
var expertPartyName="";

var fileInfo={};
var curIndex=-1;


var tmpfiles;//文件上传下载

var file = new com.im.common.file();
tmpfiles = file.activate(1);
$('#common_file').val('');
$('#common_file_name').hide();




function initTable(){

  var columns=[[
    {
      field : 'ck',
      checkbox : true,
      rowspan:2,
    },
    {field:'impleProjName',title:'实施项目名称',rowspan:2,width:$(this).width()*0.1,fitColumns:true,
      formatter: function(value,row,index){
        return row.tenderInfo.impleProjName;
      }
    },
      {field:'purchPlanName',title:"采购计划号",rowspan:2,width:$(this).width()*0.1,fitColumns:true,
          formatter: function(value,row,index){
              if(row.purchasePlan!=null){
                  return "【"+row.purchasePlan.purchPlanNum+"】"+row.purchasePlan.purchPlanName;
              } else {
                  return "";
              }

          }
      },
      {field:'purchWay',title:'采购方式',rowspan:2,width:100,fitColumns:true,
          formatter: function(value,row,index){
              if(row.purchasePlan!=null){
                  return row.purchasePlan.purchWayName;
              } else {
                  return "";
              }

          }
      },
      {field:'prepayTotal',title:"预算金额<br/>（万元）",rowspan:2,width:80,fitColumns:true,
          formatter: function(value,row,index){
              if(row.purchasePlan!=null){
                  return row.purchasePlan.prepayTotal;
              }else{
                  return "";
              }
          }},
      {field:'agentName',title:"采购代理机构",rowspan:2,width:160,fitColumns:true,
          formatter: function(value,row,index){
              if(row.purchasePlan!=null){
                  return row.purchasePlan.agentName;
              }else{
                  return "";
              }
          }},
    {field:'tenderCode',title:"标书编码",rowspan:2,fitColumns:true,width:$(this).width()*0.1,
          formatter: function(value,row,index){
              if(row.docManage!=null){
                  return "【"+row.docManage.docCode +"】"+ row.docManage.docName;
              }else{
                  return "";
              }

          }
      },
      {field:'fileList',title:"附件列表",rowspan:2,fitColumns:true,width:$(this).width()*0.1,
          formatter: function(value,row,index){
              if(row.fileUpload!=null && row.fileUpload.length > 0){
                  var html="";
                  for(var i=0;i<row.fileUpload.length;i++){
                      html+="<span style='cursor:pointer'><u><a onclick='downloadFile(\""+row.fileUpload[i].filePath+"\",\""+row.fileUpload[i].uploadName+"\")'>"+row.fileUpload[i].uploadName+"</a></u></span><br>";
                  }
                  return html;
              }else{
                  return "";
              }
          }
      },
    {field:'planType',title:"计划类型",rowspan:2,width:100,fitColumns:true,
        formatter: function(value,row,index){
            if(row.purchasePlan!=null){
                return row.purchasePlan.planTypeName;
            } else {
                return "监理招标";
            }

        }
    },
      {field:'purchaseStatus',title:"招标状态",rowspan:2,width:100,fitColumns:true,
          formatter: function(value,row,index){
              if(row.tenderInfo!=null){
                  return row.tenderInfo.statusName;
              }else{
                  return "";
              }
          }
      },
     {title:'内部评审',colspan:4},
     {title:'专家评审',colspan:4},
     {title:'标书定稿',colspan:3},
     {field:'statusCd',title:'状态',rowspan:2,width:100,fitColumns:true,
       formatter: function(value,row,index){
         if(row.docReviewResult!=null) {
           return row.docReviewResult.statusCdName;
         }else{
           return "";
         }
       }
      },
    {field:'operation',title:"操作",rowspan:2,width:$(this).width()*0.1,
      formatter: function(value,row,index){
        return '<a class="edit" href="javascript:void(0)" title="编辑" onclick="updateTenderModel('
            + index
            + ')"> <i class="fa fa-edit"></i></a>'
            +'<a class="remove" href="javascript:void(0)" title="上传"  onclick="uploadFileModel('
           + index
           +')"><i class="fa fa-upload"></i></a>';
    }},
      ],[
           {field:'revierDt1',title:'评审时间',fitColumns:true,
             formatter: function(value,row,index){
               if(row.internalReviewRecord!=null) {
                 return row.internalReviewRecord.revierDt;
               }else{
                 return "";
               }
             }
            },
            {field:'party1',title:'参与人员',fitColumns:true,
              formatter: function(value,row,index){
              return row.internalReviewers;
              }
            },
            {field:'addr1',title:'地点',width:50,fitColumns:true,
              formatter: function(value,row,index){
                if(row.internalReviewRecord!=null) {
                  return row.internalReviewRecord.addr;
                }else{
                  return "";
                }
              }
            },
              {field:'reviewResult1',title:'备注',fitColumns:true,
                  formatter: function(value,row,index){
                      if(row.internalReviewRecord!=null) {
                          return row.internalReviewRecord.reviewResult;
                      }else{
                          return "";
                      }
                  }
              },
            {field:'revierDt2',title:'评审时间',fitColumns:true,
              formatter: function(value,row,index){
                if(row.expertReviewRecord!=null) {
                  return row.expertReviewRecord.revierDt;
                }else{
                  return "";
                }
              }
            },
            {field:'party2',title:'参与人员',fitColumns:true,
              formatter: function(value,row,index){
               return row.expertReviewers;
              }
            },
            {field:'addr2',title:'地点',fitColumns:true,
              formatter: function(value,row,index){
                if(row.expertReviewRecord!=null) {
                  return row.expertReviewRecord.addr;
                }else{
                  return "";
                }
              }
            },
              {field:'reviewResult2',title:'备注',fitColumns:true,
                  formatter: function(value,row,index){
                      if(row.expertReviewRecord!=null) {
                          return row.expertReviewRecord.reviewResult;
                      }else{
                          return "";
                      }
                  }
              },
            {field:'finalDt',title:'定稿时间',fitColumns:true,
              formatter: function(value,row,index){
                if(row.docReviewResult!=null) {
                  return row.docReviewResult.finalDt;
                }else{
                  return "";
                }
              }
            },
            {field:'leadingPerson',title:'责任人',fitColumns:true,
              formatter: function(value,row,index){
                if(row.docReviewResult!=null) {
                  return row.docReviewResult.leadingPersonName;
                }else{
                  return "";
                }
              }
            },
            {field:'reviewResult3',title:'备注',fitColumns:true,
                formatter: function(value,row,index){
                    if(row.docReviewResult!=null) {
                        return row.docReviewResult.reviewResult;
                    }else{
                        return "";
                    }
                }
            },
  ]];

  $('#tenderList').datagrid({
    url:'/InfoManageWeb/docReviewService/queryDocReviewList',
    method:'post',
    dataType : 'json',
    queryParams:defaultParameter,
    pagination:true,
    nowrap:false,
    fitColumns:true,
    columns:columns,
    loadMsg:'数据加载中，请耐心等待',
    onBeforeLoad : function(param){
      console.log(param);
      for (var key in param) {
        if(key=="rows"){
          delete param[key];
        }
      }
    },
    loadFilter:function(data){
      if(data.resultCode!=0){
        toastr.error(data.resultCode);
        return null;
      }else{
        return data.result;
      }
    },
  });
  var p = $('#tenderList').datagrid('getPager');
  $(p).pagination({
    pageSize: 20,//每页显示的记录条数，默认为10
    pageList: [10,20,30],//可以设置每页记录条数的列表
    beforePageText: '第',//页数文本框前显示的汉字
    afterPageText: '页    共 {pages}  页',
    displayMsg: '当前显示 {from} - {to} 条记录 共 {total}  条记录',
    onSelectPage:function(pageNumber, pageSize){
      defaultParameter.pageNumber=pageNumber;
      defaultParameter.pageSize=pageSize;
      defaultParameter.impleProjId=$("#implProjectId").val().trim();
      defaultParameter.orgId=$("#orgId").val().trim();
      defaultParameter.planProjClass=$("#planProjClass").val().trim();
      $('#tenderList').datagrid('reload',defaultParameter);
    }
  });
}




function updateTenderModel(index){
  var row = $('#tenderList').datagrid('getData').rows[index];
 $('#tenderModal').modal("show");

    $("#orgIdInfo").val(row.tenderInfo.developOrg).change();
    $("#planProjClassInfo").val(row.tenderInfo.planProjClass).change();
    $("#implProjectInfo").val(row.purchasePlan.impleProjId).change();
// $("#projectNameText").val(row.purchasePlan.impleProjName);
 $("#tenderCode").val(row.docManage.docCode);
 $("#tenderTitle").val(row.docManage.docName);
  callBackItemProject();
  $("#purchPlanId").val(row.purchasePlan.purchPlanId).change();


  if(row.internalReviewRecord != null) {
      $("#internalRevierDt").val(row.internalReviewRecord.revierDt);
      $("#internalPartyName").val(row.internalReviewers);
      $("#internalParty").val(row.internalReviewers);
      $("#internalAddr").val(row.internalReviewRecord.addr);
      $("#internalResult").val(row.internalReviewRecord.reviewResult);
      $("#internalReviewPlanId").val(row.internalReviewPlan.reviewPlanId);
      $("#internalReviewRecordId").val(row.internalReviewRecord.reviewRecordId);
  }

    if(row.expertReviewRecord != null) {
        $("#expertRevierDt").val(row.expertReviewRecord.revierDt);
        $("#expertPartyName").val(row.expertReviewers);
        $("#expertParty").val(row.expertReviewers);
        $("#expertAddr").val(row.expertReviewRecord.addr);
        $("#expertResult").val(row.expertReviewRecord.reviewResult);
        $("#expertReviewPlanId").val(row.expertReviewPlan.reviewPlanId);
        $("#expertReviewRecordId").val(row.expertReviewRecord.reviewRecordId);
    }

    if(row.docReviewResult != null) {
        $("#finalDt").val(row.docReviewResult.finalDt);
        $("#resultLeadingPersonName").val(row.docReviewResult.leadingPersonName);
        $("#resultLeadingPerson").val(row.docReviewResult.leadingPerson);
        $("#reviewResult").val(row.docReviewResult.reviewResult);
        //$("#statusCd").val(row.docReviewResult.statusCd).trigger("change");
        $("#reviewResultId").val(row.docReviewResult.reviewResultId);
        $("#docManageId").val(row.docManage.docManageId);
    }

  disableModelField();
}

function disableModelField(){
 $("#itemBtn1").attr("disabled", true);
 $("#purchPlanId").prop("disabled", true);
 $("#orgIdInfo").prop("disabled", true);
 $("#planProjClassInfo").prop("disabled", true);
 $("#implProjectInfo").prop("disabled", true);
// $("#tenderCode").attr("readonly", true);
 $("#tenderTitle").attr("readonly", false);
}

function ableModelField(){
 $("#itemBtn1").attr("disabled", false);
 $("#purchPlanId").prop("disabled", false);
 $("#orgIdInfo").prop("disabled", false);
 $("#planProjClassInfo").prop("disabled", false);
 $("#implProjectInfo").prop("disabled", false);
 //$("#tenderCode").attr("readonly", true);
 $("#tenderTitle").attr("readonly", false);
 $("#orgIdInfo").val($("#orgId option:selected").attr("value")).change();
 $("#planProjClassInfo").val($("#planProjClass option:selected").attr("value")).change();
 $("#implProjectInfo").val($("#implProjectId option:selected").attr("value")).change();
 $("#tenderCode").val("");
    $("#tenderTitle").val("");
    $("#internalRevierDt").val("");
    $("#internalParty").val("");
    $("#internalAddr").val("");
    $("#internalResult").val("");
    $("#expertRevierDt").val("");
    $("#expertParty").val("");
    $("#expertAddr").val("");
    $("#expertResult").val("");
    $("#finalDt").val("");
    $("#resultLeadingPerson").val("");
    $("#resultLeadingPersonName").val("");
    $("#reviewResult").val("");
    $("#implProjectInfo").change();
}

function reset(){
  $("#planProjClass").val("").trigger("change");
  $("#itemOut,#itemId").val("");
}

function uploadFileModel(index){
 curIndex=index;

 $('#addFileModal').modal("show");
}

function saveUploadInfo(){
  // var formData = new FormData($('#fileForm')[0]);
    var param={};
    var row = $('#tenderList').datagrid('getData').rows[curIndex];
    var url="/docReviewService/insertFileUploadInfoForDocReview";
    var fileList = [];
    var client = new com.im.common.ajax();
    //上传文件
    var filename = $('#common_file_name label');
    var files = [];
    for (var i = 0; i < filename.length; i++) {
        if (filename.eq(i).is(':visible')) {
            files.push(filename.eq(i).html());
        }
    }

    if (filename.length > 0) {

        client.callAsFile(tmpfiles.newly, function (result) {
            if (!result.success) {
                toastr.error('文件上传失败！');
                return false;
            } else {
                $.each(result.result, function (index, item) {
                    fileList.push(item);
                });
                param.fileList = fileList;
                param.docManageId=row.docManage.docManageId;
                var client = new com.im.common.ajax();
                var result = client.callAsJson(url, param);
                if (!result.success) {
                    toastr.error(result.resultMsg);
                } else {
                    $('#addFileModal').modal("hide");
                    queryTenderInfo();
                }
            }
        });
    }
  // $.ajax({
  //   url: '/FileManageWeb/fileController/batchUploadFiles',
  //   type: 'POST',
  //   cache: false,
  //   data: formData,
  //   contentType : false,
  //   processData: false,
  //   success : function(data) {
  //     var result = JSON.parse(data);
  //     var info=result.result;
  //     console.log(result);
  //     var row = $('#tenderList').datagrid('getData').rows[curIndex];
  //     var url="/docReviewService/insertFileUploadInfoForDocReview";
      // var param={};
      // param.docManageId=row.docManage.docManageId;
      // param.uploadName=info.attachFullName;
      // param.fileSuffix=info.attachSuffix;
      // param.fileAttr=10;
      // param.uploadSize=info.attachSize;
      // param.filePath=info.attachPath;
      // param.uploadDt=info.uploadDt;
      // param.uploadDescription="";
      // var client = new com.im.common.ajax();
      // var result = client.callAsJson(url,param);
      // if(!result.success){
      //   toastr.error(result.resultMsg);
      // }else{
      //   $('#addFileModal').modal("hide");
      //   queryTenderInfo();
      // }
      // if (result.resultCode == '0') {
      //   result.success = true;
      // } else {
      //   result.success = false;
      // }
      // console.log(result);
  //   },
  // });


}



function queryTenderInfo(){
      defaultParameter.impleProjId=$("#implProjectId").val().trim();
      defaultParameter.orgId=$("#orgId").val().trim();
      defaultParameter.planProjClass=$("#planProjClass").val().trim();
    defaultParameter.purchaseStatusCd=$("#purchaseStatusCd").val().trim();
      console.log(defaultParameter);
     $('#tenderList').datagrid("load",defaultParameter);
}


function initProjectList(){
  var client = new com.im.common.ajax();
  var result = client.callAsJson("measureReportController/queryImplProjectList",null);
  console.log(result);
  if(!result.success){
    toastr.error(result.resultMsg);
  }else{
    result.result.unshift({'impleProjId':'','impleProjName':'全部'});
    $.each( result.result, function (i) {
      $('#projectName').append("<option value=" + result.result[i].impleProjId + ">" +result.result[i].impleProjName + "</option>");

    });
    $("#projectName").select2({});
    $("#projectName").val("").trigger("change");
  }

}

function initPurchPlan(param){
  $('#purchPlanName').empty();
  var client = new com.im.common.ajax();
  var result = client.callAsJson("measureReportController/queryImplProjectList",param);
  console.log(result);
  if(!result.success){
    toastr.error(result.resultMsg);
  }else{
    result.result.unshift({'purchPlanId':'','purchPlanName':'全部'});
    $.each( result.result, function (i) {
      $('#purchPlanName').append("<option value=" + result.result[i].impleProjId + ">" +result.result[i].impleProjName + "</option>");

    });
    $("#purchPlanName").select2({});
    $("#purchPlanName").val("").trigger("change");
  }

}



function initStatus(){
  var client = new com.im.common.ajax();
  var param={};
  param.tabName="T_DOC_REVIEW_RESULT";
  param.colName="STATUS_CD";
  var result = client.callAsJson("commonService/queryTabColParam",param);
  if(!result.success){
    toastr.error(result.resultMsg);
  }else{
    result.result.unshift({'colVal':'','colValName':'请选择'});
    $.each( result.result, function (i) {
      $('#statusCd').append("<option value=" + result.result[i].colVal + ">" +result.result[i].colValName + "</option>");
    });
    $("#statusCd").select2({});
  }
}

function initPurchaseStatusCd(){
    var client = new com.im.common.ajax();
    var param={};
    param.tabName="T_PURCHASE_PLAN";
    param.colName="STATUS_CD";
    var result = client.callAsJson("commonService/queryTabColParam",param);
    if(!result.success){
        toastr.error(result.resultMsg);
    }else{
        result.result.unshift({'colVal':'','colValName':'全部'});
        statusData=result.result;
        $.each( result.result, function (i) {
            $('#purchaseStatusCd').append("<option value=" + result.result[i].colVal + ">" +result.result[i].colValName + "</option>");
        });
        $("#purchaseStatusCd").select2({});
    }
}

function saveTenderInfo(){
 var reviewResultId= $("#reviewResultId").val();
  var param = {};
  var url = "/docReviewService/insertDocReviewInfo";
  if (reviewResultId != null && reviewResultId != "") {
    url = "/docReviewService/updateDocReviewInfo";
    param.docManageId=$("#docManageId").val();
    param.internalReviewPlanId=$("#internalReviewPlanId").val();
    param.internalReviewRecordId=$("#internalReviewRecordId").val();
    param.expertReviewPlanId=$("#expertReviewPlanId").val();
    param.expertReviewRecordId=$("#expertReviewRecordId").val();
    param.reviewResultId=$("#reviewResultId").val();
  }
  var formValidator = $('#tenderForm').data('bootstrapValidator');
  formValidator.options.excluded = [':disabled'];
  formValidator.validate();

  if (formValidator.isValid()) {

     param.docName=$("#tenderTitle").val();
     param.purchasePlanId=$("#purchPlanId").val();
     param.docCode=$("#tenderCode").val();
     param.impleProjId=$("#implProjectInfo").val();
     param.internalReviewPlanReviewDt=$("#internalRevierDt").val();
     param.internalReviewParty=$("#internalParty").val();
     param.internalReviewAddr=$("#internalAddr").val();
     param.internalReviewResult=$("#internalResult").val();
     param.internalReviewLinkman=$("#internalParty").val();
     param.internalReviewLeadingPerson=$("#internalParty").val();

     param.expertReviewPlanReviewDt=$("#expertRevierDt").val();
     param.expertReviewParty=$("#expertParty").val();
     param.expertReviewAddr=$("#expertAddr").val();
     param.expertReviewResult=$("#expertResult").val();
      param.expertReviewLeadingPerson=$("#expertParty").val();
      param.expertReviewLinkman=$("#expertParty").val();

     param.finalDt=$("#finalDt").val();
     param.reviewResultLeadingPerson=$("#resultLeadingPerson").val();
     param.reviewResult=$("#reviewResult").val();
     param.reviewResultStatus=$("#statusCd").val();

     console.log(param);
     var client = new com.im.common.ajax();
     var result = client.callAsJson(url,param);
      if(result.resultCode!=0){
          if(result.result != null) {
              toastr.error(result.result.message);
          } else {
              toastr.error(result.resultMsg);
          }
       }else{
          $('#tenderModal').modal('hide');
          queryTenderInfo();
       }

  }
}

function callBackItemProject(){
  $("#purchPlanId").empty();
  var projectId=$("#implProjectInfo").val();
  if(projectId == "" || projectId == null) {
      $('#purchPlanId').append("<option value =\"-1\">无采购计划</option>");
      return;
  }
  $("#projectId").val(projectId);
  //根据项目id获取采购计划
  $("#purchPlanId").empty();
  var client = new com.im.common.ajax();
  var param={
    "impleProjId":projectId,
  }
  var result = client.callAsJson("purchaseService/queryPurchasePlanByParam",param);

  console.log(result);
  if(!result.success){
    toastr.error(result.resultMsg);
  }else{
      $('#purchPlanId').append("<option value =\"-1\">无采购计划</option>");
    $.each( result.result, function (i) {
      $('#purchPlanId').append("<option value=" + result.result[i].purchPlanId + ">" +"【"+result.result[i].purchPlanNum+"】"+result.result[i].purchPlanName + "</option>");

    });
    $("#purchPlanId").select2({});
  }
}


function callBackForInteralParty(staffId){
//internalParty=internalParty+staffId+",";
//internalPartyName=internalPartyName+staffName+",";
$("#internalParty").val(staffId);
}

function callBackForLeadingPerson(staffId,staffName){
  $("#internalParty").val(staffId);
}

function callBackForExpertParty(staffId){
//  $("#expertParty").val(staffId);
//  expertParty=expertParty+staffId+",";
//  expertPartyName=expertPartyName+staffName+",";
  $("#expertParty").val(staffId);
//  $("#expertPartyName").val(expertPartyName);
}

function callBackForResultLeadingPerson(staffId,staffName){
  $("#resultLeadingPerson").val(staffId);
}

function initResultStatus(){
  var client = new com.im.common.ajax();
  var param={};
  param.tabName="T_DOC_REVIEW_PLAN";
  param.colName="STATUS_CD";
  var result = client.callAsJson("commonService/queryTabColParam",param);
  if(!result.success){
    toastr.error(result.resultMsg);
  }else{
    result.result.unshift({'colVal':'','colValName':'请选择'});
    $.each( result.result, function (i) {
      $('#internalResult').append("<option value=" + result.result[i].colVal + ">" +result.result[i].colValName + "</option>");
      $('#expertResult').append("<option value=" + result.result[i].colVal + ">" +result.result[i].colValName + "</option>");
    });
    $("#internalResult").select2({});
     $("#expertResult").select2({});
  }
}

function delTenderInfos(){
	var docManageIds = new Array();
	$.each($('#tenderList').datagrid('getChecked'), function(index, item){
		docManageIds.push(item.docManage.docManageId);
	});

	if(docManageIds.length==0){
	 toastr.info('请选中至少一行记录再删除');
	 return;
	}

    var param={};
    param.docManageIds=docManageIds.join(",");
    com.im.common.confirm('确认删除标书？',param,"deleteBatchDocManage");
}

function deleteBatchDocManage(param){
  url = "/docReviewService/deleteBatchDocManage";
  var client = new com.im.common.ajax();
  var result = client.callAsJson(url,param);
  if(!result.success) {
    toastr.error(result.resultMsg);
  }else{
//    queryPurchasePlan();
  }
}

function downloadFile(path,realName){
    location.href="/FileManageWeb/fileController/downloadFile?fileName="+path+"&realName="+encodeURI(encodeURI(realName));
}

$(function(){

     $("#orgId").project2unit2itemCommpent({
                  //elmIdProjectSel : "projectSel",//规划工程下拉元素
                  elmIdPlanTypeSel : "planProjClass",
                  elmIdUnitSel : "orgId",//组织单位下拉元素
                  elmIdItemSel : "implProjectId",//实施项目下拉元素
                  orgType : "10",//初始化单位时，单位类型
                  callbackFunc: function (callBackType, ret) {//下拉框值改变时回调
                      //_callBackOrg(objvalue, objname);
                      //callBackType : 1 所属规划值变更 2单位值变更  3项目名称变更
                      //ret: orgId,orgName,impleProjId,impleProjName,planProjClass,planProjClassName
                       queryTenderInfo();
                  }
              });

                   $("#orgIdInfo").project2unit2itemCommpent({
                                //elmIdProjectSel : "projectSel",//规划工程下拉元素
                                elmIdPlanTypeSel : "planProjClassInfo",
                                elmIdUnitSel : "orgIdInfo",//组织单位下拉元素
                                elmIdItemSel : "implProjectInfo",//实施项目下拉元素
                                orgType : "10",//初始化单位时，单位类型
                                callbackFunc: function (callBackType, ret) {//下拉框值改变时回调
                                    //_callBackOrg(objvalue, objname);
                                    //callBackType : 1 所属规划值变更 2单位值变更  3项目名称变更
                                    //ret: orgId,orgName,impleProjId,impleProjName,planProjClass,planProjClassName
                                }
                            });

//  $("#planProjClass").constantCommpent({
//    elmIdSel : "planProjClass",//需要展示的位置下拉框
//    constParamCode : "PLAN_PROJ_CLASS",//需要加载的常量编码
//    callbackFunc: function (objvalue, objname) {//下拉框值改变时回调
//    	   var planName=$("#itemOut").val().trim();
//    	   var belongPlan=$("#planProjClass").val().trim();
//    	   defaultParameter.planName=planName;
//    	   defaultParameter.belongPlan=belongPlan;
//    	   $('#tenderList').datagrid("load",defaultParameter);
//    }
//  });


//  $("#itemBtn").click(function() {
//    $("#itemBtn").itemSearchCommpent({
//      elmIdOut : "itemOut",//组织树单击之后回填的文本框（input）
//      elmIdModal : "itemSearchModal",//用户选择
//      elmIdOrgModal : "unitSearchModal",//组织查询
//      callbackFunc: function (projectId, projectName) {//组织树单击之后回调函数
//        // callBackItemProject(projectId, projectName);
//      }
//    });
//  });

   $("#implProjectInfo").on("change",function(){
      callBackItemProject();
   });

//  $("#itemBtn1").click(function() {
//    $("#itemBtn1").itemSearchCommpent({
//      elmIdOut : "projectNameText",//组织树单击之后回填的文本框（input）
//      elmIdModal : "itemSearchModal",//用户选择
//      elmIdOrgModal : "unitSearchModal",//组织查询
//      callbackFunc: function (projectId, projectName) {//组织树单击之后回调函数
//         callBackItemProject(projectId, projectName);
//      }
//    });
//  });

        $("#internalPartyBtn").click(function() {
            $("#internalPartyBtn").userSearchMutilCommpent({
                elmIdModal : "userSearchModal",//用户查询模态框
                elmIdOut : "internalPartyName",//确认时文本回填
                multiCheck : true,//false单选 true多选
                checkList : [],//已经选择的用户列表，用户取消或者删除选择[{staffId : 1,staffName : 2}]
                callbackFunc: function(objlist) {
                  var staffList="";
                   for(var i=0;i<objlist.length;i++){
                     staffList=staffList+objlist[i].staffId+",";
                   }
                   callBackForInteralParty(staffList);
                }
            });
        });
//  $("#internalPartyBtn").click(function() {
//    $("#internalPartyName").userSearchCommpent({
//      elmIdOut : "internalPartyName",//组织树单击之后回填的文本框（input）
//      elmIdModal : "userSearchModal",//用户选择
//      elmIdOrgModal : "unitSearchModal",//组织查询
//      callbackFunc: function (staffId, staffName) {//组织树单击之后回调函数
//        callBackForInteralParty(staffId, staffName);
//      }
//    });
//  });
        $("#expertPartyBtn").click(function() {
            $("#expertPartyBtn").userSearchMutilCommpent({
                elmIdModal : "userSearchModal",//用户查询模态框
                elmIdOut : "expertPartyName",//确认时文本回填
                multiCheck : true,//false单选 true多选
                checkList : [],//已经选择的用户列表，用户取消或者删除选择[{staffId : 1,staffName : 2}]
                callbackFunc: function(objlist) {
                  var staffList="";
                   for(var i=0;i<objlist.length;i++){
                     staffList=staffList+objlist[i].staffId+",";
                   }
                   callBackForExpertParty(staffList);
                }
            });
        });
//  $("#expertPartyBtn").click(function() {
//    $("#expertPartyName").userSearchCommpent({
//      elmIdOut : "expertPartyName",//组织树单击之后回填的文本框（input）
//      elmIdModal : "userSearchModal",//用户选择
//      elmIdOrgModal : "unitSearchModal",//组织查询
//      callbackFunc: function (staffId, staffName) {//组织树单击之后回调函数
//        callBackForExpertParty(staffId, staffName);
//      }
//    });
//  });

  $("#leadingPersonBtn").click(function() {
    $("#leadingPersonName").userSearchCommpent({
      elmIdOut : "leadingPersonName",//组织树单击之后回填的文本框（input）
      elmIdModal : "userSearchModal",//用户选择
      elmIdOrgModal : "unitSearchModal",//组织查询
      callbackFunc: function (staffId, staffName) {//组织树单击之后回调函数
        callBackForLeadingPerson(staffId, staffName);
      }
    });
  });

  $("#resultLeadingPersonBtn").click(function() {
    $("#resultLeadingPersonBtn").userSearchCommpent({
      elmIdOut : "resultLeadingPersonName",//组织树单击之后回填的文本框（input）
      elmIdModal : "userSearchModal",//用户选择
      elmIdOrgModal : "unitSearchModal",//组织查询
      callbackFunc: function (staffId, staffName) {//组织树单击之后回调函数
        callBackForResultLeadingPerson(staffId, staffName);
          $('#tenderForm').data('bootstrapValidator').updateStatus('resultLeadingPerson', 'NOT_VALIDATED',null).validateField('resultLeadingPerson');
      }
    });
  });

  initTable();

  initProjectList();

  initStatus();

  initPurchaseStatusCd();

  //initResultStatus();

    $("#purchaseStatusCd").change(function(){
        queryTenderInfo();
    });


  $('#myFile').change(function (){
          	var reg = /[^\\\/]*[\\\/]+/g;
   $('#myFileName').val($(this).val().replace(reg, ''));
  });

  $("#uploadBtn").click(function(){
    saveUploadInfo();
  });

  $("#implProjectList").bind('change', function () {
    initPurchPlan({impleProjId: $(this).val()});
  });

  $("#purchPlanName").bind('change', function () {
      initTenderInfo({purchPlanId: $(this).val()});
    });

  $("#ensureBtn").click(function () {
    saveTenderInfo();
  });


 $("#search-btn").click(function(){
   var planName=$("#itemOut").val().trim();
   var belongPlan=$("#planProjClass").val().trim();
   defaultParameter.planName=planName;
   defaultParameter.belongPlan=belongPlan;
   $('#tenderList').datagrid("load",defaultParameter);
 });

 $("#btn_reset").click(function(){
   reset();
 });

 $("#btn_del").click(function(){
  delTenderInfos();
  queryTenderInfo();
 });

 $("#btn_add").click(function(){
   $('#tenderModal').modal({
     backdrop: false,//点击空白处不关闭对话框
     keyboard: false,//键盘关闭对话框
     show:true//弹出对话框
   });
   ableModelField();

 });

  $("#implProjectInfo").on("change", function () {
    var impleProjectId= $(this).val();
    if(impleProjectId!=undefined && impleProjectId!=null && impleProjectId.trim()!=""){
      var client = new com.im.common.ajax();
      var param={};
      param.seqType="3";
      param.seqArg=impleProjectId;
      var result = client.callAsJson("commonService/getSeqForRule",param);
      if(result.resultCode!=0){
        toastr.error(result.resultCode);
      }else{
        console.log(result.result.seq);
        $("#tenderCode").val(result.result.seq);
        $('#tenderForm').data('bootstrapValidator').updateStatus('tenderCode', 'NOT_VALIDATED',null).validateField('tenderCode');
      }
    }else{
      $("#tenderCode").val('');
    }


  });

 $('#tenderModal').on('hidden.bs.modal', function () {
   internalParty="";
   internalPartyName="";
   $("#purchPlanId").empty();
   $("#orgIdInfo").val("").trigger("change");
   $("#planProjClassInfo").val("").trigger("change");
   $("#implProjectInfo").val("").trigger("change");
   $("#purchPlanId").val("").trigger("change");
   $("#internalResult").val("");
   $("#expertResult").val("");
   $("#statusCd").val("").trigger("change");
   $('#tenderForm').data('bootstrapValidator').options.excluded = [':disabled'];
   $('#tenderForm').bootstrapValidator('resetForm', true);
   $('#tenderForm')[0].reset();
 });

  //时间插件
  $('.datetimepicker').datepicker({
      onSelect:function (){
      	$('.datetimepicker').trigger("change");
      }
  });

  var needCheckTenderCode = "false";

  $("#tenderCode").on("blur",(function(){
      needCheckTenderCode = "true";
      $('#tenderForm').data('bootstrapValidator').updateStatus('tenderCode', 'NOT_VALIDATED',null).validateField('tenderCode');
  }));

  $("#tenderCode").on("focus",(function(){
      needCheckTenderCode = "false";
  }));

    $("#tenderForm").bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-loadData'
        },
        excluded: [':disabled', ':not(:visible)'],
        fields: {
            /*purchPlanId: {
                validators: {
                    notEmpty: {
                        message: '请选择采购计划'
                    }
                }
            },*/
            tenderTitle: {
                validators: {
                    notEmpty: {
                        message: '标书名称不能为空'
                    }
                }
            },
            tenderCode: {
                validators: {
                    notEmpty: {
                        message: '标书编码不能为空'
                    },
                    callback:{
                        callback : function(value, validator, $field) {
                            if (needCheckTenderCode == "true" && $.trim(value) !== ''){
                                needCheckTenderCode = "false";
                                var client = new com.im.common.ajax();
                                var param={};
                                param.docCode = value;
                                var result = client.callAsJson("docReviewService/queryHadDocCode",param);
                                if(!result.success || result.result.had == "true") {
                                    return {
                                        valid: false,
                                        message: '标书编码重复'
                                    }
                                } else {
                                    return true
                                }
                            }
                            else{
                                return true
                            }
                        }
                    }
                }
            }
        }
    });


})